package com.tywj.navigation.map

import android.graphics.Rect
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.tywj.panda.customer.common.widget.recyclerview.MyBaseAdapter
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.core.widget.recycleview.adapter.BaseViewHolder
import com.tywj.navigation.R
import com.tywj.navigation.entity.NavigationApplyHistoryEntity

/**
 * 我的申请适配器
 */
class NavigationApplyCourseAdapter :
    MyBaseAdapter<NavigationApplyHistoryEntity>(R.layout.navigation_recyclerview_item_apply_courses) {

    override fun convert(helper: BaseViewHolder, item: NavigationApplyHistoryEntity) {
        super.convert(helper, item)

        helper.setText(R.id.tvLineBusName, item.busLineName)
        helper.setText(R.id.tvTime, item.time)
        helper.setText(R.id.tvRemark, item.remark)

        val textNumTips = ResourcesUtil.getString(R.string.navigation_apply_num)
        val textNumUnit = ResourcesUtil.getString(R.string.navigation_apply_num_unit)

        val numStr = SpannableStringBuilder()
            .append(textNumTips)
            .append(item.num.toString())
            .append(textNumUnit)

        numStr.setSpan(
            ForegroundColorSpan(ResourcesUtil.getColor(R.color.common_red_FF)),
            textNumTips.length,
            textNumTips.length + item.num.toString().length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        helper.setText(R.id.tvNum, numStr)
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {

                val position = parent.getChildLayoutPosition(view)
                if (position != RecyclerView.NO_POSITION) {
                    when (position) {
                        // 第一个
                        0 -> {
                            outRect.top = ResourcesUtil.getDimensionPixelSize(R.dimen.dp_12)
                            outRect.bottom = ResourcesUtil.getDimensionPixelSize(R.dimen.dp_12)
                        }
                        // footer TODO:待测试
                        this@NavigationApplyCourseAdapter.data.size - 1 + this@NavigationApplyCourseAdapter.headerLayoutCount -> outRect.top =
                            0
                        // 其他
                        else -> outRect.bottom = ResourcesUtil.getDimensionPixelSize(R.dimen.dp_12)
                    }
                }
            }
        })
    }
}